package com.server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.dao.OrderInfo;
import com.dao.UserInfo;
import com.dbserver.Condb;

public class OrderListService {
	public ArrayList<OrderInfo> getOrder(String date, int userid, int type , int shopid,int pageNum,int pageSize)
	   {   
		ArrayList<OrderInfo> orderList=new ArrayList<OrderInfo>();
		 //处理结果集
	   	Condb condb=new Condb();
	   	int BeginRecord=(pageNum-1)*pageSize;
	   	String sql="select * from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id";
	   	if(date!=""||userid!=-1||type!=-1||shopid!=-1)
	   	{
	   		sql+=" where";
	   		if(date!="")
		   	{
		   		sql+=" Date(ordertime)='"+date+"'+";
		   		if(userid!=-1)
		   		{
		   			sql+=" and o.userid="+userid;
		   		}
		   		if(type!=-1)
		   		{
		   			sql+=" and p.type="+type;
		   		}
		   		if(shopid!=-1)
		   		{
		   			sql+=" and si.id="+shopid;
		   		}
		   	}else {
		   		if(userid!=-1)
		   		{
		   			sql+=" o.userid="+userid;
		   			if(type!=-1)
			   		{
			   			sql+=" and p.type="+type;
			   		}
			   		if(shopid!=-1)
			   		{
			   			sql+=" and si.id="+shopid;
			   		}
		   		}else {
		   			if(type!=-1)
			   		{
			   			sql+=" p.type="+type;
			   			if(shopid!=-1)
				   		{
				   			sql+=" and si.id="+shopid;
				   		}
			   		}else {
			   			if(shopid!=-1)
				   		{
				   			sql+=" si.id="+shopid;
				   		}
					}
			   		
				}
		   		
			}
	   		
	   	}
	   	

        sql+=" limit "+BeginRecord+","+pageSize;
	   	ResultSet rs=condb.query(sql);
		try {
	
			while(rs.next())
			{   
				OrderInfo orderInfo=new OrderInfo();
				orderInfo.setId(rs.getInt("id"));
				orderInfo.setOrderitemid(rs.getString("orderitemid"));
				orderInfo.setUserid(rs.getInt("userid"));
				orderInfo.setComment(rs.getString("comment"));
				orderInfo.setConfirmDate(rs.getDate("confirmtime"));
				orderInfo.setCredit(rs.getInt("credit"));
				orderInfo.setOrderDate(rs.getDate("ordertime"));
				orderInfo.setPaserviceid(rs.getInt("paserviceid"));
				orderInfo.setStar(rs.getInt("star"));
				orderInfo.setStatus(rs.getInt("status"));
				orderList.add(orderInfo);
			}
			rs.close();
			condb.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderList;
	   }
}
